// component/index.js
const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
     list:{
       type:Array,
       value:""
     },
     callBackPage:{type:String,value:""}
  },

  /**
   * 组件的初始数据
   */
  data: {
    alpha: '',
    windowHeight: '',
    addBg: false,
    categoryParentList: {}, //品种-父类
    categoryParentChildList: {}, //品种-子类
    selected_categoryParent: 0, //
    selected_categoryChild: {}, //
  },

  ready:function(){
    try {
      var res = wx.getSystemInfoSync();
      //console.log(res.windowHeight)
      //每一个字母所占的高度
      this.apHeight = res.windowHeight / 26;
      this.setData({ windowHeight: res.windowHeight });
      this.getList();

    } catch (e) {
      
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //选中类别
    onItemClick(e){
      //console.log(e.currentTarget.dataset.name)
      var that = this;

      that.triggerEvent('sureSelectCategory', e);
    },
    //点击
    handlerAlphaTap(e) {
      let { ap } = e.target.dataset;
      this.setData({ alpha: ap });
      
    },
    //滑动
    handlerMove(e) {
      let { list } = this.data;
      this.setData({ addBg: true });
      let rY = e.touches[0].clientY;//竖向滑动的距离
      if (rY >= 0) {
        let index = Math.ceil((rY - this.apHeight) / this.apHeight);
        if (0 <= index < list.length) {
          let nonwAp = list[index];
          nonwAp && this.setData({ alpha: nonwAp.alphabet });
        }
      }
    },
    //滑动结束
    handlerEnd(e) {
      this.setData({ addBg: false });
    },
    //加载类别列表
    getList:function(e)
    {
      var that = this;
      //发起网络请求
      http.post(appConfig.baseUrl + 'Notice/GetCategoryListByCategoryType',
        { CategoryType: 0 }, function (res) {
          console.log(res.data);
          if (res && res.data && res.data.length > 0) {
            that.setData({
              list: res.data,
            });

          }
        });
      //加载品种列表
      http.post(
        appConfig.baseUrl + 'Notice/GetCategoryListByParentId', {
          ParentId: 0
        },
        function (res) {
          that.setData({
            categoryParentList: res.data
          });
        });
    },
    selectCategoryParent: function (e) {
    var that = this;
    wx.showLoading({
      title: '玩命加载中...',
    });
    that.setData({
      selected_categoryParent: e.currentTarget.dataset.category,
      categoryParentChildList:{}
    });
    http.post(appConfig.baseUrl + 'Notice/GetCategoryListByCategoryType',
      { CategoryType: e.target.dataset.category }, function (res) {
        that.setData({
          list: res.data,
        });
        wx.hideLoading(); //关闭加载动画
      });
    }
  }
})
